// src/store/index.js

import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    user: '',
    isLoggedIn: false,
    menus: {
      public: [
        {
          key: '0',
          title: '首页',  
          route: '/index'
        },
        {
          key: '11',
          title: '地图',  
          route: '/map'
        },
        {
          key: 'sc',
          title: '市场信息',  
          route: '/sc'
        }

      ],
      farmer: [
        {
          key: '1',
          title: '农场',  
          route: '/farmer'
        },
        {
          key: 'product',
          title: '供应',  
          route: '/product'
        },
      ],
      company: [
        {
          key: '1',
          title: '公司',
          route: '/company'
        },
        // {
        //   key: '2',
        //   title: '市场信息',
        //   route: '/sc'
        // },
        {
          key: '3',
          title: '需求',
          route: '/demand'
        },
      ]
    }
  },
  mutations: {
    setUser(state, user) {
      state.user = user;
      state.isLoggedIn = !!user;
    },
    logout(state) {
      state.user = '';
      state.isLoggedIn = false;
    }
  },
  actions: {
    login({ commit }, user) {
      commit('setUser', user);
    },
    setUserRole({ commit }, role) {
      const user = { role: role };
      commit('setUser', user);
    },
    login2({ commit }, { token, role }) {
      const user = { token, role };
      commit('setUser', user);
    },
    logout({ commit }) {
      commit('logout');
    }
  },
  getters: {
    currentUser: state => {
      return state.user || 'Guest'; // 如果没有用户信息，则显示 "Guest"
    },
    userMenus: state => {
      const publicMenus = state.menus.public || [];
      const roleMenus = state.menus[state.user.role] || [];
      return publicMenus.concat(roleMenus);
    },
    isLoggedIn(state) {
      return state.isLoggedIn;
    }

  }
});
